package com.szx.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szx.usercenter.domain.SysRoleMenu;
import com.szx.usercenter.mapper.SysRoleMenuMapper;
import com.szx.usercenter.service.SysRoleMenuService;
import com.szx.usercenter.util.Response;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author yikonsh
 * @description 针对表【sys_role_menu】的数据库操作Service实现
 * @createDate 2024-05-21 14:20:18
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService {

  @Override
  public Response addRoleMenu(SysRoleMenu sysRoleMenu) {
    LambdaQueryWrapper<SysRoleMenu> qw = new LambdaQueryWrapper<>();
    qw.eq(SysRoleMenu::getRoleId, sysRoleMenu.getRoleId());
    this.remove(qw);

    boolean save = this.save(sysRoleMenu);
    return save ? Response.success() : Response.error();
  }

  @Override
  public Response getCheckedKeys(Long roleId) {
    LambdaQueryWrapper<SysRoleMenu> qw = new LambdaQueryWrapper<>();
    qw.eq(SysRoleMenu::getRoleId, roleId);
    SysRoleMenu sysRoleMenu = this.getOne(qw);

    if (sysRoleMenu != null) {
      return Response.rspData(sysRoleMenu.getCheckedKeys());
    } else {
      return Response.rspData(new ArrayList<>());
    }
  }
}
